﻿using System;
using System.Data.Linq;

namespace RomanJendrusz.Amazon.ETL.Domain
{
    public class BookAuthors : IEquatable<BookAuthors>
    {
        private EntityRef<Author> author;


        private int authorId;
        private EntityRef<Book> book;
        private string bookAsin;
        private DateTime updated;

        public BookAuthors()
        {
            book = new EntityRef<Book>();
            author = new EntityRef<Author>();
        }

        /// <summary>
        /// Gets or sets the row updated.
        /// </summary>
        /// <value>The row updated.</value>
        public DateTime Updated
        {
            get { return updated; }
        }

        public Book Book
        {
            get { return book.Entity; }
            set
            {
                book.Entity = value;
                if(value != null)
                bookAsin = value.Asin;
            }
        }

        public Author Author
        {
            get { return author.Entity; }
            set
            {
                author.Entity = value;
                if(value != null)
                authorId = value.AuthorId;
            }
        }

        #region IEquatable<BookAuthors> Members

        public bool Equals(BookAuthors bookAuthors)
        {
            if (bookAuthors == null) return false;
            return Equals(bookAsin, bookAuthors.bookAsin) && authorId == bookAuthors.authorId;
        }

        #endregion

        public override bool Equals(object obj)
        {
            if (ReferenceEquals(this, obj)) return true;
            return Equals(obj as BookAuthors);
        }

        public override int GetHashCode()
        {
            return base.GetHashCode();
        }
    }
}